package com.aat.dxfy.redis.pool;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public  class PropertiesUtil {
	private String propertiesFileName = null;
	private Properties props = new Properties();

	
	public PropertiesUtil(String propertiesFileName) {
		this.propertiesFileName = propertiesFileName;
		init();
	}

	private URL getStorageConfigURI() throws FileNotFoundException {

		ClassLoader loader = PropertiesUtil.class.getClassLoader();
		URL scpurl = loader.getResource(getPropertiesFile());
		if (scpurl != null)
			return scpurl;

		throw new FileNotFoundException("Cannot locate " + getPropertiesFile());
	}

	private void init(){
		try {
			URL configFileURI = getStorageConfigURI();
			props.load(configFileURI.openStream());
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

	public String getProperty(String key, String defaultValue) {
		if (props.containsKey(key)) {
			return props.getProperty(key, defaultValue);
		}
		return System.getProperty(key, defaultValue);
	}

	private String getPropertiesFile() {
		return propertiesFileName;
	}

	public void setPropertiesFile(String filename) {
		propertiesFileName = filename;
	}

}
